package com.ctsi.ssdc.entity.Excel08VO;
import cn.hutool.core.bean.BeanUtil;
import com.ctsi.ssdc.entity.Excel08VO.Excel08VO;

import com.ctsi.ssdc.annocation.AutoId;
import com.ctsi.ssdc.entity.Excel08;
import com.ctsi.ssdc.util.LongtoStringSerialize;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import liquibase.pro.packaged.D;
import lombok.Data;
import org.apache.logging.log4j.util.PropertySource;
import org.springframework.context.annotation.Bean;
import org.springframework.format.annotation.DateTimeFormat;

import javax.lang.model.type.ArrayType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Excel08VO {

    /**
     * 序列号
     */
    @AutoId(primaryKey = "id")
    @JsonSerialize(using = LongtoStringSerialize.class)
    private Long id;
    /**
     * 单位名称
     */
    private String name;
    /**
     * 工业产值本期当月
     */
    private Double gyczCurrentmonth;
    /**
     * 工业产值同期当月
     */
    private Double gyczPreviousmonth;
    /**
     * 工业产值本期累计
     */
    private Double gyczCurrentaccumulate;
    /**
     * 工业产值同期累计
     */
    private Double gyczPreviousaccumulate;
    /**
     * 工业产值当月同比
     */
    private Double gyczMonthonmonth1;
    /**
     * 工业产值累计同比
     */
    private Double gyczMonthonmonth2;
    /**
     * 归属集团/同一法人
     */
    private String group;
    /**
     *  子公司
     */
    private List<Excel08VO> children;
    /**
     * 类型 0集团 1法人
     */
    private Integer apiType;
    /**
     * 日期
     */
    @JsonFormat(pattern = "yyyy-MM",timezone = "GMT+8")
    @DateTimeFormat(pattern="yyyy-MM")
    private Date statisticalTime;

    public static Excel08VO voBuilder(String year,String month, String name, Integer apiType, List<Excel08> list) {
        Excel08VO excel08VO = new Excel08VO();
        excel08VO.setName(name);
        list = list.stream().sorted(Comparator.comparing(Excel08::getGyczCurrentaccumulate, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        Double[] sum = list.stream()
                .map(e -> new Double[]{e.getGyczCurrentmonth(), e.getGyczPreviousmonth(),
                        e.getGyczCurrentaccumulate(), e.getGyczPreviousaccumulate()})
                .reduce((x, y) -> {
                    x[0] += y[0];
                    x[1] += y[1];
                    x[2] += y[2];
                    x[3] += y[3];
                    return x;
                }).get();
        excel08VO.setApiType(apiType);
        excel08VO.setGyczCurrentmonth(sum[0]);
        excel08VO.setGyczPreviousmonth(sum[1]);
        double monthOnMonth1 = Math.round(((excel08VO.getGyczCurrentmonth() / excel08VO.getGyczPreviousmonth()) - 1) * 1000) / 10.0;
        excel08VO.setGyczMonthonmonth1(monthOnMonth1);
        excel08VO.setGyczCurrentaccumulate(sum[2]);
        excel08VO.setGyczPreviousaccumulate(sum[3]);
        double monthOnMonth2 = Math.round(((excel08VO.getGyczCurrentaccumulate() / excel08VO.getGyczPreviousaccumulate()) - 1) * 1000) / 10.0;
        excel08VO.setGyczMonthonmonth2(monthOnMonth2);
        List<Excel08VO> children = list.stream().map(e -> {
            Excel08VO child = new Excel08VO();
            BeanUtil.copyProperties(e, child);
            child.setApiType(2);
            return child;
        }).collect(Collectors.toList());
        excel08VO.setChildren(children);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        try {
            excel08VO.setStatisticalTime(simpleDateFormat.parse(year+"-"+month));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return excel08VO;
    }

    public static List<Excel08VO> copyList(List<Excel08> list) {
        List<Excel08VO> excel08VOList = list.stream().map(e -> {
            Excel08VO excel08VO = new Excel08VO();
            BeanUtil.copyProperties(e, excel08VO);
            return excel08VO;
        }).collect(Collectors.toList());
        return excel08VOList;
    }

}
